package main

import (
	"fmt"
	"sort"
	"strings"
)

/*
把数组排成最小的数
输入一个非负整数数组，把数组里所有数字拼接起来排成一个数，打印能拼接出的所有数字中最小的一个。

示例 1:
输入: [10,2]
输出: "102"

示例2:
输入: [3,30,34,5,9]
输出: "3033459"
 */

func minNumber(nums []int) string {
	sort.Slice(nums, func(i, j int) bool {
		return compareNumber(nums[i],nums[j])
	})
	var res strings.Builder
	for i:=0;i<len(nums);i++{
		res.WriteString(fmt.Sprintf("%d",nums[i]))
	}
	return res.String()
}

func compareNumber(a,b int)bool{
	str1 := fmt.Sprintf("%d%d",a,b)
	str2 := fmt.Sprintf("%d%d",b,a)
	if str1<str2{
		return true
	}
	return false
}

//冒泡排序
/*
func minNum(nums []int)string{
	for i:=0;i<len(nums)-1;i++{//外循环
		for j:=0;j<len(nums)-1;j++{
			if !compare(nums[j],nums[j+1]){
				nums[j],nums[j+1] = nums[j+1],nums[j]
			}
		}
	}
	var res string
	for _,v:=range nums{
		res = res + strconv.Itoa(v)
	}
	return res
}

func compare(a,b int)bool{
	str1:=fmt.Sprintf("%d%d",a,b)
	str2:=fmt.Sprintf("%d%d",b,a)
	return str1 < str2
}
 */

//快速排序
/*
func minNum(nums []int)string{
	nums = quick(nums,0,len(nums)-1)
	var res string
	for _,v:=range nums{
		res = res + strconv.Itoa(v)
	}
	return res
}

func quick(arr []int,start,end int)[]int{
	if start < end {
		i,j := start,end
		mid :=arr[(start+end)/2]
		for i <= j{
			for compare(arr[i],mid){
				i++
			}
			for compare(mid,arr[j]){
				j--
			}
			if i<=j{
				arr[i], arr[j] = arr[j], arr[i]
				i++
				j--
			}
		}
		if start < j{
			quick(arr,start,j)
		}
		if end > i{
			quick(arr,i,end)
		}
	}
	return arr
}

func compare(a,b int)bool{
	str1:=fmt.Sprintf("%d%d",a,b)
	str2:=fmt.Sprintf("%d%d",b,a)
	return str1 < str2
}
 */

func main() {
	nums:=[]int{3,30,34,5,9}
	fmt.Println(minNumber(nums))
}
